package com.weiliang.www;

import java.util.Scanner;
import java.util.Stack;

/**
 * @Author: liangWeiQUQ
 * @Description: 有效括号
 * @DateTime: 2021/5/31 13:12
 **/
public class isValid20 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你要判断的字符串");
        String s = scanner.nextLine();
        boolean b = isValid(s);
        System.out.println(b);

    }

    private static boolean isValid(String s) {
        Stack<Character>stack = new Stack<Character>();
        for(char c: s.toCharArray()){
            if(c=='(')
            {
                stack.push(')');
            }
            else if(c=='['){stack.push(']');}
            else if(c=='{'){stack.push('}');}
            //当不是左边的（{[的时候说明是右边的)}],则弹出栈内存储的元素和C比较是否相等
            else if(stack.isEmpty()||c!=stack.pop()){return false;}
        }
        return stack.isEmpty();
    }
}
